﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Input;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using Corina.Commands;

namespace Corina.Cydee.Apphub.Viewmodels
{
    public class RoleViewmodel : ViewmodelBase
    {
        public RoleViewmodel()
        {
            ManagerClient.GetAllRoleCompleted += GetAllRoleCompleted;
            ManagerClient.InsertRoleCompleted += InsertRoleCompleted;
            ManagerClient.UpdateRoleCompleted+=UpdateRoleCompleted;
            if (DataContext.IsValid)
            {
                LoadData();
            }
        }

        private void UpdateRoleCompleted(object sender, DataService.UpdateRoleCompletedEventArgs e)
        {
            LoadData();
            UpdateEntity = new RoleInfo();
            Operation = EntityOperation.Insert;
        }

        private void InsertRoleCompleted(object sender, DataService.InsertRoleCompletedEventArgs e)
        {
            LoadData();
            UpdateEntity = new RoleInfo();
            Operation = EntityOperation.Insert;
        }

        private void GetAllRoleCompleted(object sender, DataService.GetAllRoleCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                if (e.Result.Result == ResultValue.Succeed)
                {
                    ItemsCollection = new ObservableCollection<RoleInfo>(e.Result.Entities.OfType<RoleInfo>());
                }
            }
            else
            {
                MessageBox.Show(e.Error.Message);
            }
            IsBusy = false;
        }

        protected override void LoadData(params object[] paras)
        {
            IsBusy = true;
            ManagerClient.GetAllRoleAsync(Account, Password);
        }

        #region Properties
        
        private ObservableCollection<RoleInfo> _itemsCollections;
        public ObservableCollection<RoleInfo> ItemsCollection
        {
            get { return _itemsCollections; }
            set
            {
                _itemsCollections = value;
                RaisePropertyChanged(() => ItemsCollection);
            }
        }

        private RoleInfo _updateEntity = new RoleInfo();
        public RoleInfo UpdateEntity
        {
            get { return _updateEntity; }
            set
            {
                _updateEntity = value;
                RaisePropertyChanged(() => UpdateEntity);
            }
        }

        #endregion

        #region Commands
        public ICommand EditCommand
        {
            get
            {
                return new DelegateCommand(t =>
                {
                    int id = Convert.ToInt32(t);
                    UpdateEntity = ItemsCollection.FirstOrDefault(i => i.Id == id);
                    Operation = EntityOperation.Update;
                });
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate {
                        UpdateEntity = new RoleInfo();
                        Operation = EntityOperation.Insert;
                    }
                };
            }
        }

        public ICommand SubmitCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate {
                        if(Operation == EntityOperation.Update)
                        {
                            ManagerClient.UpdateRoleAsync(Account, Password, UpdateEntity.Name, UpdateEntity.Code, UpdateEntity.Id);
                        }
                        else
                        {
                            ManagerClient.InsertRoleAsync(Account, Password, UpdateEntity.Name, UpdateEntity.Code);
                        }
                    }
                };
            }
        }
        #endregion
    }
}
